package com.inspur.edp.rule.dsl.core.function.list;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2025/1/23 15:56
 */
public class JsonArrayUtils {


    public static int size(ArrayNode arrayNode) {
        if (arrayNode == null) {
            return 0;
        }
        return arrayNode.size();
    }

    public static Number sumByField(ArrayNode arrayNode, String fieldName) {
        int size = size(arrayNode);
        if (size == 0) {
            return 0;
        }
        double total = 0;
        for (JsonNode jsonNode : arrayNode) {
            double value = jsonNode.get(fieldName).asDouble(0);
            total += value;
        }
        return total;
    }


    public static void sortByField(ArrayNode arrayNode, String fieldName, boolean asc) {
        throw new IllegalArgumentException("json array not support sort .");
    }

}
